package com.arcsoft.arcfacedemo.activity;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.arcsoft.arcfacedemo.R;
import com.arcsoft.arcfacedemo.common.Constants;
import com.arcsoft.arcfacedemo.util.ConfigUtil;
import com.arcsoft.face.ActiveFileInfo;
import com.arcsoft.face.ErrorInfo;
import com.arcsoft.face.FaceEngine;

import rx.Observable;
import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;


/**
 * 主界面
 */
public class ChooseFunctionActivity extends AppCompatActivity {
    private static final String     TAG                        = "ChooseFunctionActivity";
    private              Toast      toast                      = null;

    private              FaceEngine faceEngine                 = new FaceEngine();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_choose_function);
        initView();
    }

    private void initView() {
        //设置视频模式下的人脸优先检测方向
        RadioGroup radioGroupFtOrient = findViewById(R.id.radio_group_ft_orient);
        RadioButton rbOrient0 = findViewById(R.id.rb_orient_0);
        RadioButton rbOrient90 = findViewById(R.id.rb_orient_90);
        RadioButton rbOrient180 = findViewById(R.id.rb_orient_180);
        RadioButton rbOrient270 = findViewById(R.id.rb_orient_270);
        RadioButton rbOrientAll = findViewById(R.id.rb_orient_all);

        //体重秤的选择角度应该为90度

        //初始化选择
        rbOrient90.setChecked(true);
        ConfigUtil.setFtOrient(ChooseFunctionActivity.this, FaceEngine.ASF_OP_90_ONLY);


        switch (ConfigUtil.getFtOrient(this)) {
            case FaceEngine.ASF_OP_0_ONLY:
                rbOrient0.setChecked(true);
                break;
            case FaceEngine.ASF_OP_90_ONLY:
                rbOrient90.setChecked(true);
                break;
            case FaceEngine.ASF_OP_180_ONLY:
                rbOrient180.setChecked(true);
                break;
            case FaceEngine.ASF_OP_270_ONLY:
                rbOrient270.setChecked(true);
                break;
            case FaceEngine.ASF_OP_0_HIGHER_EXT:
                rbOrientAll.setChecked(true);
                break;
            default:
                rbOrient0.setChecked(true);
                break;
        }
        radioGroupFtOrient.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.rb_orient_0:
                        ConfigUtil.setFtOrient(ChooseFunctionActivity.this, FaceEngine.ASF_OP_0_ONLY);
                        break;
                    case R.id.rb_orient_90:
                        ConfigUtil.setFtOrient(ChooseFunctionActivity.this, FaceEngine.ASF_OP_90_ONLY);
                        break;
                    case R.id.rb_orient_180:
                        ConfigUtil.setFtOrient(ChooseFunctionActivity.this, FaceEngine.ASF_OP_180_ONLY);
                        break;
                    case R.id.rb_orient_270:
                        ConfigUtil.setFtOrient(ChooseFunctionActivity.this, FaceEngine.ASF_OP_270_ONLY);
                        break;
                    case R.id.rb_orient_all:
                        ConfigUtil.setFtOrient(ChooseFunctionActivity.this, FaceEngine.ASF_OP_0_HIGHER_EXT);
                        break;
                    default:
                        ConfigUtil.setFtOrient(ChooseFunctionActivity.this, FaceEngine.ASF_OP_0_ONLY);
                        break;
                }
            }
        });


        activeEngine(findViewById(R.id.active));//启动立刻激活跳转人脸识别
    }


    // TODO: 2019/7/1 此界面跳转到实时人脸识别!

    /**
     * 打开相机，显示年龄性别
     *
     * @param view
     */
    public void jumpToPreviewActivity(View view) {
        startActivity(new Intent(this, PreviewActivity.class));
    }


    /**
     * 激活引擎 按键时间
     *
     * @param view
     */
    public void activeEngine(final View view) {


//        Observable.create(new ObservableOnSubscribe<Integer>() {
//            @Override
//            public void subscribe(ObservableEmitter<Integer> emitter) throws Exception {
//
//                //在线激活人脸识别引擎
//                int activeCode = new FaceEngine().activeOnline(ChooseFunctionActivity.this, Constants.APP_ID, Constants.SDK_KEY);
//                emitter.onNext(activeCode);
//            }
//        })
//                .subscribeOn(Schedulers.io())
//                .observeOn(AndroidSchedulers.mainThread())
//                .subscribe(new Observer<Integer>() {
//                    @Override
//                    public void onSubscribe(Disposable d) {
//
//                    }
//
//                    @Override
//                    public void onNext(Integer activeCode) {
//                        if (activeCode == ErrorInfo.MOK) {
//                            showToast("激活引擎成功");
//                            startActivity(new Intent(ChooseFunctionActivity.this, PreviewActivity.class));
//
//                        } else if (activeCode == ErrorInfo.MERR_ASF_ALREADY_ACTIVATED) {
//                            showToast("引擎已激活，无需再次激活");
//                            startActivity(new Intent(ChooseFunctionActivity.this, PreviewActivity.class));
//                        } else {
//                            Log.d(TAG, "onNext: 引擎激活失败，错误码为 " + activeCode);
//                            showToast(getString(R.string.active_failed, activeCode));
//                        }
//
//                        if (view != null) {
//                            view.setClickable(true);
//                        }
//                        ActiveFileInfo activeFileInfo = new ActiveFileInfo();
//                        //激活文件信息类
//                        int res = faceEngine.getActiveFileInfo(ChooseFunctionActivity.this, activeFileInfo);
//                        if (res == ErrorInfo.MOK) {
//                            Log.e(TAG, "激活文件信息类:" + activeFileInfo.toString());
//                        }
//                    }
//
//                    @Override
//                    public void onError(Throwable e) {
//
//                    }
//
//                    @Override
//                    public void onComplete() {
//
//                    }
//                });
    }





    private void showToast(String s) {
        if (toast == null) {
            toast = Toast.makeText(this, s, Toast.LENGTH_SHORT);
            toast.show();
        } else {
            toast.setText(s);
            toast.show();
        }
    }

}
